 
package com.cy.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cy.tool.FilterParam;
import com.cy.tool.Page;


public class HibernateDao extends HibernateDaoSupport {
	//多条件查询
	protected List findIn(List<FilterParam> filters, Class object) {
		Criteria criteria = parseCriteria(filters, object);
		return criteria.list();

	}
	//用于分页
	protected Page pageQueryIn(List<FilterParam> filter, int pageNo,
			int pageSize, Class object) {
		try {
			pageNo = pageNo > 0 ? pageNo : 1;
			Criteria criteria = parseCriteria(filter, object);
			int totalCount = criteria.list().size();
			criteria.setFirstResult(pageSize * (pageNo - 1));
			criteria.setMaxResults(pageSize);
			Page page = new Page(criteria.list(), pageNo, pageSize, totalCount);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//合并查询条件
	private Criteria parseCriteria(List<FilterParam> filters, Class object) {
		Criteria criteria = this.getSession().createCriteria(object);
		System.out.println(criteria);
		List<Criteria> criteriaList = new ArrayList<Criteria>();
		for (FilterParam f : filters) {
			if (f.key.indexOf(".") < 1) {
				setSingleCriteria(criteriaList, criteria, f);
			} else {
				int index = f.key.indexOf(".");
				int criteriaIndex = Integer.parseInt(f.key.substring(index-1, index));
				f.key = f.key.substring(index+1, f.key.length());
				setSingleCriteria(criteriaList,
						criteriaList.get(criteriaIndex-1), f);
			}
		}
		return criteria;
	}

	private Criteria setSingleCriteria(List<Criteria> criteriaList,
			Criteria criteria, FilterParam f) {
		switch (f.type) {
		case NE:
			criteria.add(Restrictions.ne(f.key, f.value));
			break;
		case EQ:
			criteria.add(Restrictions.eq(f.key, f.value));
			break;
		case LT:
			criteria.add(Restrictions.lt(f.key, f.value));
			break;
		case LE:
			criteria.add(Restrictions.le(f.key, f.value));
			break;
		case GT:
			criteria.add(Restrictions.gt(f.key, f.value));
			break;
		case GE:
			criteria.add(Restrictions.ge(f.key, f.value));
			break;
		case LIKE:
			criteria.add(Restrictions.like(f.key, "%" + f.value + "%"));
			break;
		case ASC:
			criteria.addOrder(Order.asc(f.key));
			break;
		case DESC:
			criteria.addOrder(Order.desc(f.key));
			break;
		case JION:
			setCreateCriteria(criteriaList, criteria, f);
			break;
		}
		return criteria;
	}

	private void setCreateCriteria(List<Criteria> criteriaList,
			Criteria criteria, FilterParam f) {
				Criteria criteriaOther = criteria.createCriteria(f.key,
						CriteriaSpecification.LEFT_JOIN);
				criteriaList.add(criteriaOther);
	}
	@SuppressWarnings("unchecked")
	public List<String> getDataBaseConnInfo()
	{
		Configuration configuration =  new Configuration();
		configuration.configure();
		
		String url=configuration.getProperty("connection.url");//当前数据库连接url
		String cls=configuration.getProperty("connection.driver_class");
		String username=configuration.getProperty("connection.username");
		String password=configuration.getProperty("connection.password");
		List<String> r = new ArrayList();
		r.add(url);
		r.add(cls);
		r.add(username);
		r.add(password);
		return r;
	}
}
